//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "TimeBasket.h"
using namespace Cephei::QL::Cashflows;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ values) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<DateTime>^ _Cdates;
    CoVector<Double>^ _Cvalues;
    try
    {
#ifdef HANDLE
        _phTimeBasket = NULL;
#endif
        CoVector<DateTime>^ _Cdates = safe_cast<CoVector<DateTime>^> (dates);
        _Cdates->Lock();
        INativeVector<DateTime>^ _NCIdates = _Cdates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        CoVector<Double>^ _Cvalues = safe_cast<CoVector<Double>^> (values);
        _Cvalues->Lock();
        INativeVector<Double>^ _NCIvalues = _Cvalues->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvalues = safe_cast<CDoubleVector^>(_NCIvalues);
        std::vector<QuantLib::Real>& _values = static_cast<std::vector<QuantLib::Real>&> (_NCvalues->GetReference ());
        _ppTimeBasket = new boost::shared_ptr<QuantLib::TimeBasket> (new QuantLib::TimeBasket ( _dates,  _values ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdates != nullptr) _Cdates->Unlock();
        if (_Cvalues != nullptr) _Cvalues->Unlock();
    }
}
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phTimeBasket = NULL;
#endif
        _ppTimeBasket = new boost::shared_ptr<QuantLib::TimeBasket> (new QuantLib::TimeBasket ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (boost::shared_ptr<QuantLib::TimeBasket>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeBasket = NULL;
#endif
	_ppTimeBasket = &childNative;
    
}
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (QuantLib::TimeBasket& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTimeBasket = NULL;
#endif
	_ppTimeBasket = new boost::shared_ptr<QuantLib::TimeBasket> (&childNative);
    
    _TimeBasketOwner = owner;
    
}

Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (CTimeBasket^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeBasket = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTimeBasket = new boost::shared_ptr<QuantLib::TimeBasket> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeBasket = NULL;
#endif
	if (!t->IsSubclassOf(CTimeBasket::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (QuantLib::Handle<QuantLib::TimeBasket>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTimeBasket = &childNative;
	_ppTimeBasket = &static_cast<boost::shared_ptr<QuantLib::TimeBasket>>(childNative.currentLink());
    
    _TimeBasketOwner = owner;
}
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (QuantLib::Handle<QuantLib::TimeBasket> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTimeBasket = &childNative;
	_ppTimeBasket = &static_cast<boost::shared_ptr<QuantLib::TimeBasket>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CTimeBasket::CTimeBasket (QuantLib::TimeBasket childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeBasket = NULL;
#endif
	_ppTimeBasket = new boost::shared_ptr<QuantLib::TimeBasket> (new QuantLib::TimeBasket (childNative));
    
}
#endif

Cephei::QL::Cashflows::CTimeBasket::~CTimeBasket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTimeBasket != NULL)
    {
	    delete _ppTimeBasket;
        _ppTimeBasket = NULL;
    }
}
Cephei::QL::Cashflows::CTimeBasket::!CTimeBasket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTimeBasket != NULL)
    {
	    delete _ppTimeBasket;
    }
}
QuantLib::TimeBasket& Cephei::QL::Cashflows::CTimeBasket::GetReference ()
{
    if (_ppTimeBasket == NULL) throw gcnew NativeNullException ();
	return **_ppTimeBasket;
}
boost::shared_ptr<QuantLib::TimeBasket>& Cephei::QL::Cashflows::CTimeBasket::GetShared ()
{
    if (_ppTimeBasket == NULL) throw gcnew NativeNullException ();
	return *_ppTimeBasket;
}
QuantLib::TimeBasket* Cephei::QL::Cashflows::CTimeBasket::GetPointer ()
{
    if (_ppTimeBasket == NULL) throw gcnew NativeNullException ();
	return &**_ppTimeBasket;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TimeBasket>& Cephei::QL::Cashflows::CTimeBasket::GetHandle ()
{
	if (_phTimeBasket == NULL)
	{
		_phTimeBasket = new Handle<QuantLib::TimeBasket> (*_ppTimeBasket);
	}
	return *_phTimeBasket;
}
#endif
bool Cephei::QL::Cashflows::CTimeBasket::HasNative () 
{
	return (_ppTimeBasket != NULL);
}

Boolean Cephei::QL::Cashflows::CTimeBasket::HasDate (DateTime prm1)
{
    try
    {
        QuantLib::Date _prm1 = (QuantLib::Date)ValueHelper::Convert (prm1);
    	bool _rv = (bool)(*_ppTimeBasket)->hasDate ( _prm1 );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::ITimeBasket^ Cephei::QL::Cashflows::CTimeBasket::Rebin (Cephei::IVector<DateTime>^ buckets)
{
    CoVector<DateTime>^ _Cbuckets;
    try
    {
        _Cbuckets = safe_cast<CoVector<DateTime>^> (buckets);
        _Cbuckets->Lock();
        INativeVector<DateTime>^ _NCIbuckets = _Cbuckets->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCbuckets = safe_cast<CDateTimeVector^>(_NCIbuckets);
        std::vector<QuantLib::Date>& _buckets = static_cast<std::vector<QuantLib::Date>&> (_NCbuckets->GetReference ());
    	QuantLib::TimeBasket _rv = (QuantLib::TimeBasket)(*_ppTimeBasket)->rebin ( _buckets );   
        Cephei::QL::Cashflows::CTimeBasket^ _nrv = gcnew Cephei::QL::Cashflows::CTimeBasket (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbuckets != nullptr) _Cbuckets->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ITimeBasket^ Cephei::QL::Cashflows::CTimeBasket_Factory::Create (Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ values)
{
    return gcnew CTimeBasket ( dates,  values);
}
Cephei::QL::Cashflows::ITimeBasket^ Cephei::QL::Cashflows::CTimeBasket_Factory::Create ()
{
    return gcnew CTimeBasket ();
}
